Write a program to implement operator overloading (unary and binary operators)
🔷 Operator Overloading in C++: Unary and Binary Operators
In C++, operator overloading allows you to redefine the way operators work for user-defined types (like classes). This is especially useful when working with complex data structures or mathematical objects such as complex numbers, vectors, or matrices.
By default, C++ only knows how to apply operators like `+`, `-`, `++`, `--` to built-in types (like `int`, `float`). But with operator overloading, we can extend this functionality to user-defined types, enhancing code readability and usability.
✅ Types of Operators You Can Overload
- Unary operators – operate on a single operand (e.g., `-`, `++`, `--`)
- Binary operators – operate on two operands (e.g., `+`, `-`, `*`, `/`)
Let's look at how both unary and binary operator overloading works in C++.
🧑💻 C++ Program: Operator Overloading (Unary and Binary)
#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
// Constructor
Number(int v = 0) {
value = v;
}
// Display method
void display() {
cout << "Value: " << value << endl;
}
// Unary minus operator overloading
Number operator-() {
return Number(-value);
}
// Binary plus operator overloading
Number operator+(Number n) {
return Number(value + n.value);
}
};
int main() {
Number n1(10), n2(20), result;
// Unary operator
Number neg = -n1; // calls operator-()
cout << "Unary Operator Overloading:" << endl;
neg.display(); // Output: -10
// Binary operator
result = n1 + n2; // calls operator+()
cout << "Binary Operator Overloading:" << endl;
result.display(); // Output: 30
return 0;
}
🖨️ Output
Unary Operator Overloading:
Value: -10
Binary Operator Overloading:
Value: 30
📝 Code Explanation
The class `Number` contains a private data member `value` and two overloaded operators:
1. Unary minus (-): This operator is overloaded using the `operator-()` function to negate the value of the object.
2. Binary plus (+): This operator is overloaded using the `operator+(Number)` function to add the values of two `Number` objects.
In `main()`, we create two `Number` objects (`n1` and `n2`), use `-n1` to demonstrate unary operator overloading, and `n1 + n2` to demonstrate binary operator overloading.
🎯 Benefits of Operator Overloading
- Makes user-defined objects behave more like built-in types.
- Enhances readability and maintainability of code.
- Essential when designing mathematical or data structure classes.
⚠️ Things to Keep in Mind
- Not all operators can be overloaded (e.g., `::`, `.*`, `sizeof`, `?:`).
- Overloading should not be abused—it should preserve intuitive behavior.
- Always test for edge cases like negative values, large numbers, etc.
📌 Conclusion
Operator overloading is a powerful tool in C++ that allows developers to make classes more intuitive and expressive. Understanding how to overload unary and binary operators provides a strong foundation for building robust and reusable object-oriented applications.
Comments
Post a Comment